import { ref } from 'vue'

const key = '__theme__'
const theme = ref(sessionStorage.getItem(key) || 'light')
document.documentElement.dataset.theme = theme.value

// 切换主题
const toggleTheme = () => {
  theme.value = theme.value === 'light' ? 'dark' : 'light'
  document.documentElement.dataset.theme = theme.value
  sessionStorage.setItem(key, theme.value)
}

// 主题切换  light | dark
export function useTheme() {
  return { theme, toggleTheme }
}
